package com.zl.rbac.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.rbac.entity.Permission;
import com.zl.rbac.entity.Role;
import com.zl.rbac.service.IPermissionService;
import com.zl.rbac.service.IRoleService;
import com.zl.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色的控制器
 * @ClassName: RoleController
 * @author: suke
 * @since: 2021/1/12 9:22
 */
@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService  permissionService;

    @GetMapping("list")
    @RequiredPermission("查询角色列表")
    public String  list(@RequestParam(defaultValue = "1") Integer pageIndex, Model model) throws Exception {
        Page<Role>  page = roleService.queryByPage(pageIndex,1);
        model.addAttribute("page", page);
        return "role/list";
    }



    @GetMapping("save")
    @RequiredPermission("进入角色添加页面")
    public String  toSavePage(Model model)  throws  Exception{
        //查询所有权限, 把权限保存request域
        List<Permission> permissionList =  permissionService.queryAll();
        model.addAttribute("unSelectedPermissions",permissionList);
        return "role/edit";
    }


    /**
     * 
     * @Title: 处理添加与修改的方法
     * @param role  角色对象
     * @param permissionIds  选择的一组权限id
     * @return java.lang.String
     * @author: suke
     * @since: 2021/1/12 10:09
     */
    @PostMapping("saveOrUpdate")
    @RequiredPermission("角色添加与修改")
    public String  saveOrUpdate(Role role, Integer[] permissionIds) throws Exception{
        //区分是添加还是修改,  判断role的id是否有值,  有: 修改   没有: 添加
        if(role.getId() == null || role.getId() ==  0){  // 添加
            roleService.save(role,permissionIds);
        }else{ //修改
            roleService.update(role,permissionIds);
        }
        return  "redirect:/role/list";  //重定向到    public String  list 处理方法
    }


    @GetMapping("update")
    @RequiredPermission("进入角色修改页面")
    public String  toPpdatePage(Integer roleid,Model model)  throws  Exception{

        //查询修改的角色的信息
        Role role = roleService.queryRoleAndPermissionsById(roleid);
        model.addAttribute("role",role);

        //角色拥有的权限
        List<Permission> selectedPermissions =role.getPermissionList();

        //查询未选权限, 1) 编写sql查询  SELECT *  FROM tb_permission  WHERE id  NOT IN()
        //  2) 代码逻辑判断
        List<Permission> permissionList =  permissionService.queryAll();
        if(role.getPermissionList().size() == 0){
            model.addAttribute("unSelectedPermissions",permissionList);
        }else{
            //创建未选的权限集合
            List<Permission> unSelectedPermissions = new ArrayList<>();
            for(Permission permission: permissionList){
                if(!selectedPermissions.contains(permission)){
                    unSelectedPermissions.add(permission);
                }
            }
            model.addAttribute("unSelectedPermissions",unSelectedPermissions);
        }
        //查询角色已选权限, 已知条件:  角色id

        return "role/edit";
    }
}
